/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.click.app.webapp.action;

import com.click.app.model.FeatureProduct;
import com.click.app.model.FeatureProductName;
import com.click.app.model.FeatureProductDescription;
import com.click.app.model.Product;
import com.click.app.service.FeatureProductManager;
import com.click.app.service.FeatureProductNameManager;
import com.click.app.service.FeatureProductDescriptionManager;
import com.click.app.service.GenericManager;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author orgale
 */
public class FeatureProductAction extends BaseAction {

    private FeatureProductManager featureProductManager;
    private GenericManager<Product, Long> productManager;
    private FeatureProductNameManager featureProductNameManager;
    private FeatureProductDescriptionManager featureProductDescriptionManager;
    private List featureProducts;
    private FeatureProduct featureProduct;
    private Long id;
    private String name;

    public void setId(Long id) {
        this.id = id;
    }

    public FeatureProduct getFeatureProduct() {
        return featureProduct;
    }

    public void setFeatureProduct(FeatureProduct featureProduct) {
        this.featureProduct = featureProduct;
    }

    public boolean productsFree(FeatureProduct dis) {
        boolean resp = true;

        List<Product> mats = productManager.getAll();
        for (Product m : mats) {
            for (FeatureProduct d : m.getListFeatureProducts()) {
                if (d.equals(dis)) {
                    resp = false;

                }
            }
        }

        return resp;
    }

    public String delete() {
        if (productsFree(featureProduct)) {
            featureProductManager.remove(new Long(featureProduct.getId()));
            saveMessage(getText("featureProduct.deleted"));
        } else {
            List messages = (List) getRequest().getSession().getAttribute("errors");
            if (messages == null) {
                messages = new ArrayList();
            }
            messages.add(getText("featureProduct.deleteError"));
            getRequest().getSession().setAttribute("errors", messages);

        }
        return SUCCESS;
    }

    public String edit() {
        if (id != null) {
            featureProduct = featureProductManager.get(id);
        } else {
            featureProduct = new FeatureProduct();
        }

        return SUCCESS;
    }

    public FeatureProduct getFeatureProductWithNameAndDescription(String n, String v) {
        FeatureProduct d = null;
        for (FeatureProduct di : featureProductManager.getAll()) {
            if ((di.getDescription().matches(v)) && (di.getName().matches(n))) {
                d = di;
            }
        }
        return d;
    }

    public boolean validationPasses() {
        boolean vp = true;
        if (featureProduct.getName() == null || featureProduct.getName().matches("")) {
            errorMessage("Required field, name.");
            vp = false;
        }
        if (featureProduct.getDescription() == null || featureProduct.getDescription().matches("")) {
            errorMessage("Required field, description.");
            vp = false;
        }
        return vp;
    }

    public String save() throws Exception {
        if (cancel != null) {
            return CANCEL;
        }

        if (delete != null) {
            return delete();
        }

        boolean isNew = (featureProduct.getId() == null);

        FeatureProductName dn = new FeatureProductName();
        FeatureProductDescription dv = new FeatureProductDescription();
        if ((getFeatureProductWithNameAndDescription(featureProduct.getName(), featureProduct.getDescription())) != null) {
            errorMessage("Product feature already exists.");
            System.out.println("FeatureProduct already exists...");

        } else {
            boolean exists = false;
            for (FeatureProductName d : featureProductNameManager.getAll()) {
                if (featureProduct.getName().matches(d.getName())) {
                    dn = d;
                    exists = true;
                }
            }
            System.out.println("FeatureProductName already exists...:" + exists);
            //if featureProductname exists
            if (exists) {
            } else {
                dn.setName(featureProduct.getName());
                dn.setNameEs(featureProduct.getNameEs());
                dn = featureProductNameManager.save(dn);
            }

            //is featureProduct is new
            dv.setName(featureProduct.getDescription());
            dv.setNameEs(featureProduct.getDescriptionEs());
            dv.setFeatureProductName(dn);

            dv = featureProductDescriptionManager.save(dv);
            dn.getFeatureProductDescriptions().add(dv);

            //FeatureProductDescription dn = new FeatureProductName();
            //dn.setName(featureProduct.getName());
            if (validationPasses()) {
                featureProduct = featureProductManager.save(featureProduct);
            } else {
                return ERROR;
            }
            String key = (isNew) ? "featureProduct.added" : "featureProduct.updated";
            saveMessage(getText(key));
        }
        if (!isNew) {
            return INPUT;
        } else {
            return SUCCESS;
        }
    }

    public void setFeatureProductManager(FeatureProductManager featureProductManager) {
        this.featureProductManager = featureProductManager;
    }

    public void setFeatureProductNameManager(FeatureProductNameManager featureProductNameManager) {
        this.featureProductNameManager = featureProductNameManager;
    }

    public void setFeatureProductDescriptionManager(FeatureProductDescriptionManager featureProductDescriptionManager) {
        this.featureProductDescriptionManager = featureProductDescriptionManager;
    }

    public void setProductManager(GenericManager<Product, Long> productManager) {
        this.productManager = productManager;
    }

    public List getFeatureProducts() {
        return featureProducts;
    }

    public String list() {
        featureProducts = featureProductManager.getAll();
        return SUCCESS;
    }

    public String helpList() {
        return SUCCESS;
    }

    public String help() {
        return SUCCESS;
    }

    public List<FeatureProductName> getDNames() {
        List<FeatureProductName> dNames = new ArrayList<FeatureProductName>();
        return featureProductNameManager.getAll();
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    public String saveFeatureProductDescription() {
        System.out.println("NAME: " + name);
        System.out.println("id: " + getRequest().getAttribute("id"));
        System.out.println("d: " + getRequest().getAttribute("featureProduct"));

        return SUCCESS;
    }
    private String featureProductName;
    private String featureProductDescription;

    public String deleteFeatureProductDescription() {
        if (productsFree(featureProductManager.findFeatureProductsByNameAndDescription(getFeatureProductName(), getFeatureProductDescription()).get(0))) {
            featureProductManager.remove((featureProductManager.findFeatureProductsByNameAndDescription(getFeatureProductName(), getFeatureProductDescription())).get(0).getId());
            featureProductDescriptionManager.remove(featureProductDescriptionManager.findFeatureProductDescriptionsByName(getFeatureProductDescription()).getId());
            FeatureProductName dn = featureProductNameManager.findFeatureProductNamesByName(getFeatureProductName());
            if (dn.getFeatureProductDescriptions().isEmpty()) {
                featureProductNameManager.remove(featureProductNameManager.findFeatureProductNamesByName(getFeatureProductName()).getId());
            }
            saveMessage(getText("featureProduct.deleted"));
        } else {
            List messages = (List) getRequest().getSession().getAttribute("errors");
            if (messages == null) {
                messages = new ArrayList();
            }
            messages.add(getText("featureProduct.deleteError"));
            getRequest().getSession().setAttribute("errors", messages);

        }
        return SUCCESS;
    }

    public List<FeatureProductName> getFeatureProductNames() {
        return featureProductNameManager.getAll();
    }

    /**
     * @return the featureProductName
     */
    public String getFeatureProductName() {
        return featureProductName;
    }

    /**
     * @param featureProductName the featureProductName to set
     */
    public void setFeatureProductName(String featureProductName) {
        this.featureProductName = featureProductName;
    }

    /**
     * @return the featureProductDescription
     */
    public String getFeatureProductDescription() {
        return featureProductDescription;
    }

    /**
     * @param featureProductDescription the featureProductDescription to set
     */
    public void setFeatureProductDescription(String featureProductDescription) {
        this.featureProductDescription = featureProductDescription;
    }
}